package org.example;

import java.io.*;
import java.net.InetSocketAddress;
import java.net.Socket;

public class TraditionalIOSender {
    public static void main(String[] args) throws Exception {
        Socket socket = new Socket();
        socket.connect(new InetSocketAddress("localhost", 7001));
        String filename = "C:\\Users\\25459\\Desktop\\study\\my_netty\\src\\main\\resources\\test";

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(filename));
        OutputStream os = socket.getOutputStream();

        byte[] buffer = new byte[8192];  // 8KB 缓冲区
        int bytesRead;

        long startTime = System.currentTimeMillis();
        long totalBytesSent = 0;

        while ((bytesRead = bis.read(buffer)) != -1) {
            os.write(buffer, 0, bytesRead);
            totalBytesSent += bytesRead;
        }
        // 确保数据全部写出
        os.flush();

        long endTime = System.currentTimeMillis();

        System.out.println("发送的总字节数 = " + totalBytesSent + "，耗时 = " + (endTime - startTime) + " ms");

        bis.close();
        os.close();
        socket.close();
    }
}
